package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/mixin"
)

// SysUser holds the schema definition for the SysUser entity.
type SysUser struct {
	ent.Schema
}

// Annotations of the SysUser.
func (SysUser) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{Table: "sys_user"},
	}
}

// Fields of the SysUser.
func (SysUser) Fields() []ent.Field {
	return []ent.Field{
		field.String("user_name").SchemaType(map[string]string{
			dialect.MySQL: "varchar(60)", // Override MySQL.
		}).Comment("用户名").Unique(),
		field.String("uuid").Comment("账户uuid").Unique(),
		field.String("password").SchemaType(map[string]string{
			dialect.MySQL: "varchar(64)", // Override MySQL.
		}).Comment("密码"),
		field.String("nick_name").SchemaType(map[string]string{
			dialect.MySQL: "varchar(64)", // Override MySQL.
		}).Comment("昵称"),
		field.String("avatar").SchemaType(map[string]string{
			dialect.MySQL: "varchar(255)", // Override MySQL.
		}).Comment("头像").Optional(),
		field.String("phone").SchemaType(map[string]string{
			dialect.MySQL: "varchar(20)", // Override MySQL.
		}).Comment("手机号").Optional(),
		field.String("email").SchemaType(map[string]string{
			dialect.MySQL: "varchar(50)", // Override MySQL.
		}).Comment("邮箱").Optional(),
		field.String("remark").SchemaType(map[string]string{
			dialect.MySQL: "varchar(255)", // Override MySQL.
		}).Optional().Comment("备注"),
		field.Int8("user_type").SchemaType(map[string]string{
			dialect.MySQL: "tinyint(4)", // Override MySQL.
		}).Default(2).Comment("用户类型 1 管理员  2   非管理员"),
		field.String("address").SchemaType(map[string]string{
			dialect.MySQL: "varchar(255)", // Override MySQL.
		}).Optional().Comment("联系地址"),
		field.String("last_login_ip").SchemaType(map[string]string{
			dialect.MySQL: "varchar(15)", // Override MySQL.
		}).Optional().Default("").Comment("最后登录ip"),
		field.Time("last_login_time").SchemaType(map[string]string{
			dialect.MySQL: "datetime", // Override MySQL.
		}).Optional().Comment("最后登录时间"),
		field.Time("delete_time").SchemaType(map[string]string{
			dialect.MySQL: "datetime", // Override MySQL.
		}).Optional().Comment("删除时间"),
		field.Int("company_id").Comment("公司ID").Optional(),
		field.Int("role_id").Comment("角色ID").Optional(),
	}
}

// Edges of the SysUser.
func (SysUser) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("company", SysCompany.Type).
			Ref("users").
			Unique().Field("company_id"),
		edge.From("role", SysRole.Type).
			Ref("users").
			Unique().Field("role_id"),
	}
}

// Mixin of the SysUser.
func (SysUser) Mixin() []ent.Mixin {
	return []ent.Mixin{
		mixin.Time{},
	}
}
